<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('供货商编码')">
              <a-input :placeholder="$t('请输入供货商编码')" v-model="queryParam.code"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('供货商名称')">
              <a-input :placeholder="$t('请输入供货商名称')" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('类型')">
                <j-dict-select-tag
                  :placeholder="$t('请选择类型')"
                  v-model="queryParam.type"
                  dictCode="bis_goods_supplier.type"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="!tenantid">
              <a-form-item :label="$t('门店')">
                <a-select v-model="queryParam.tenantId">
                  <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value"
                    >{{ item.title }}<span v-if="item.value != ''">（{{ item.value }}）</span></a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'BisGoodsSupplierController:add'">{{ $t('新增') }}</a-button>
      <a-button type="primary" icon="download" :loading="exloading" @click="handleExportXls($t('供应商'))" v-has="'BisGoodsSupplierController:export'">{{ $t('导出') }}</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{ $t('下载') }}
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record" v-if="(!record.tenantId&&!tenantid)||(record.tenantId==tenantid)">
          <a @click="handleEdit(record)" v-has="'BisGoodsSupplierController:edit'">{{ $t('编辑') }}</a>
          <a-divider type="vertical" v-has="'BisGoodsSupplierController:edit'" />
          <a-popconfirm :title="$t('确定删除吗?')" @confirm="() => handleDelete(record.id)">
            <a v-has="'BisGoodsSupplierController:delete'">{{ $t('删除') }}</a>
          </a-popconfirm>
          <a-divider type="vertical" v-has="'BisGoodsSupplierController:delete'" />
          <a @click="handleDetail(record)">{{ $t('详情') }}</a>
          <a-divider type="vertical" v-if="record.type == 1" />
          <a v-if="record.type == 1" @click="handleQrcode(record)">{{ $t('商家码') }}</a>
        </span>
      </a-table>
    </div>

    <bis-goods-supplier-modal ref="modalForm" @ok="modalFormOk"></bis-goods-supplier-modal>
    <a-modal
      class="modal_title"
      :title="$t('商家码')"
      :visible="qrVisible"
      @cancel="handleCancel"
      :cancel-text="$t('关闭')"
    >
      <qrcode-model
        v-if="qrVisible"
        :info="info"
        :param="'deviceId'"
        :paramValue="'id'"
      ></qrcode-model>
      <template slot="footer">
        <a-button type="default" @click="handleCancel">{{$t('关闭')}}</a-button>
      </template>
    </a-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import Vue from 'vue'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { ACCESS_TOKEN, TENANT_ID } from '@/store/mutation-types'
import { filterMultiDictText, initDictOptions } from '@/components/dict/JDictSelectUtil'
import BisGoodsSupplierModal from './modules/BisGoodsSupplierModal'
import QrcodeModel from './modules/QrcodeModel'

export default {
  name: 'BisGoodsSupplierList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    BisGoodsSupplierModal,
    QrcodeModel
  },
  data() {
    return {
      description: '供应商管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: this.$t('供货商编码'),
          align: 'center',
          dataIndex: 'code',
        },
        {
          title: this.$t('供货商名称'),
          align: 'center',
          dataIndex: 'name',
        },
        {
          title: this.$t('类型'),
          align: 'center',
          dataIndex: 'type_dictText',
        },
        {
          title: this.$t('图片'),
          align: 'center',
          dataIndex: 'picture',
          scopedSlots: { customRender: 'imgSlot' },
        },
        {
          title: this.$t('联系人'),
          align: 'center',
          dataIndex: 'contacts',
        },
        {
          title: this.$t('联系电话'),
          align: 'center',
          dataIndex: 'phone',
        },
        {
          title: this.$t('微信'),
          align: 'center',
          dataIndex: 'wechat',
        },
        {
          title: this.$t('qq'),
          align: 'center',
          dataIndex: 'qq',
        },
        {
          title: this.$t('联系地址'),
          align: 'center',
          dataIndex: 'address',
        },
        {
          title: this.$t('备注'),
          align: 'center',
          dataIndex: 'comment',
        },
        {
          title: this.$t('开户银行'),
          align: 'center',
          dataIndex: 'bank',
        },
        {
          title: this.$t('银行卡号'),
          align: 'center',
          dataIndex: 'bankNo',
        },
        {
          title: this.$t('操作'),
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/goods/bisGoodsSupplier/list',
        delete: '/goods/bisGoodsSupplier/delete',
        deleteBatch: '/goods/bisGoodsSupplier/deleteBatch',
        exportXlsUrl: '/goods/bisGoodsSupplier/exportXls',
        importExcelUrl: 'goods/bisGoodsSupplier/importExcel',
      },
      dictOptions: {},
      superFieldList: [],      
      qrVisible:false,
      info:{},
      shops:[],
      tenantid: Vue.ls.get(TENANT_ID),
      queryParam:{
        tenantId:''
      }
    }
  },
  created() {
    if (!this.tenantid) {
      initDictOptions('bis_shop,name,code').then((res) => {
        if (res.success) {
          this.shops = res.result
          this.shops.unshift({ value: 0, title: this.$t('总部') })
          this.shops.unshift({ value: '', title: this.$t('全部') })
        }
      })
      this.columns.splice(1,0,{
        title: this.$t('所属门店'),
        align: 'left',
        dataIndex: 'tenantId',
        customRender: (t, r) => {
          let str
          if (!t) {
            str = this.$t('总部')
          } else {
            str = r.tenantId_dictText ? t + ' ' + r.tenantId_dictText : t
          }
          return str
        },
      })
    }
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'code', text: this.$t('供货商编码'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'name', text: this.$t('供货商名称'), dictCode: '' })
      fieldList.push({ type: 'int', value: 'type', text: this.$t('类型'), dictCode: 'bis_goods_supplier.type' })
      fieldList.push({ type: 'string', value: 'picture', text: this.$t('图片'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'contacts', text: this.$t('联系人'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'phone', text: this.$t('联系电话'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'wechat', text: this.$t('微信'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'qq', text: this.$t('qq'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'address', text: this.$t('联系地址'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'comment', text: this.$t('备注'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'bank', text: this.$t('开户银行'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'bankNo', text: this.$t('银行卡号'), dictCode: '' })
      this.superFieldList = fieldList
    },
    handleQrcode(record){
      this.qrVisible = true
      this.info = record
    },
    handleCancel(){
      this.qrVisible = false
    },
    searchReset(){
      this.queryParam={
        tenantId:''
      }
      this.searchQuery()
    }
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>